﻿using System.Windows.Controls;
using System.Windows;
using System;

namespace EveIntel.WpfApplication
{
    /// <summary>
    /// Interaction logic for WebBrowserControl.xaml
    /// </summary>
    public partial class WebBrowserControl : UserControl
    {
        public static DependencyProperty SourceUriProperty = DependencyProperty.Register("SourceUri", typeof(Uri), typeof(WebBrowserControl), new PropertyMetadata(null, new PropertyChangedCallback(OnSourceUriChanged)));
        public static DependencyProperty IsLoadingProperty = DependencyProperty.Register("IsLoading", typeof(bool), typeof(WebBrowserControl));

        public WebBrowserControl()
        {
            InitializeComponent();
            browser.LoadCompleted += (s, e) =>
                                         {
                                             IsLoading = false;
                                         };
        }
        
        public Uri SourceUri
        {
            get { return GetValue(SourceUriProperty) as Uri; }
            set { SetValue(SourceUriProperty,value); }
        }

        public bool IsLoading
        {
            get { return (bool) GetValue(IsLoadingProperty); }
            set { SetValue(IsLoadingProperty,value); }
        }

        private static void OnSourceUriChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
        {
            ((WebBrowserControl) sender).IsLoading = true;
            ((WebBrowserControl)sender).browser.Navigate(e.NewValue as Uri);
        }
    }
}
